package cn.stylefeng.guns.modular.manager.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.FlowBill;
import cn.stylefeng.guns.modular.manager.model.params.FlowBillParam;
import cn.stylefeng.guns.modular.manager.service.FlowBillService;
import cn.stylefeng.guns.sys.modular.system.entity.FlowDeptStore;
import cn.stylefeng.guns.sys.modular.system.service.FlowDeptStoreService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 发票控制器
 *
 * @author yumker
 * @Date 2021-01-07 17:22:24
 */
@Controller
@RequestMapping("/flowBill")
public class FlowBillController extends BaseController {

    private String PREFIX = "/manager/flowBill";

    @Autowired
    private FlowBillService flowBillService;
    @Autowired
    private FlowDeptStoreService flowDeptStoreService;

    /**
     * 跳转到主页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/flowBill.html";
    }

    /**
     * 新增页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/flowBill_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/flowBill_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FlowBillParam flowBillParam) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        //查询storeid
        QueryWrapper<FlowDeptStore> flowDeptStoreQueryWrapper = new QueryWrapper<>();
        flowDeptStoreQueryWrapper.eq("dept_id",user.getDeptId());
        flowDeptStoreQueryWrapper.last("limit 1");
        FlowDeptStore flowDeptStore = flowDeptStoreService.getOne(flowDeptStoreQueryWrapper);
        if(flowDeptStore != null){
            flowBillParam.setStoreId(flowDeptStore.getStoreId());
        }else {
            if(!user.getAccount().equals("admin")){
                return ResponseData.error("您未有绑定storeid，请联系管理员");
            }
        }
        this.flowBillService.add(flowBillParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FlowBillParam flowBillParam) {
        this.flowBillService.update(flowBillParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FlowBillParam flowBillParam) {
        this.flowBillService.delete(flowBillParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FlowBillParam flowBillParam) {
        FlowBill detail = this.flowBillService.getById(flowBillParam.getBillId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FlowBillParam flowBillParam) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(!user.getAccount().equals("admin")){
            flowBillParam.setDeptId(user.getDeptId());
        }
        return this.flowBillService.findPageBySpec(flowBillParam);
    }

}


